﻿var app = angular.module('app', []);

/*
app.factory('LectureList', function ($scope, $http) {
    return {
        list: [
            {
                id: 0,
                name: "Lecture 1",
                link: "http://www.youtube.com/embed/uYZy9eRazVw",
                comments: ["hello", "ja", "saell"]
            }, {
                id: 1,
                name: "Lecture 2",
                link: "http://www.youtube.com/embed/rrOuJY3G5fU",
                comments: ["va", "shitt thetta er langt comment, veit ekki hvort thad virki en samt va...!!!!"]
            }
        ]
    };

});
*/
app.config(function ($routeProvider) {
    $routeProvider
		.when('/',
		{ templateUrl: "../content/lectures.html", controller: "LecturesController" })
		.when('/lecture/:id',
		{ templateUrl: "../content/lecture.html", controller: "LectureController" })
        .when('/editLecture/:id',
        { templateUrl: "../content/editLecture.html", controller: "editLectureController" })
		.otherwise(
			{ template: "End of the multiverse!!!!" })
    ;
});





app.controller('LecturesController', ["$location", "$http", '$scope', function ($location, $http, $scope) {
    $scope.lectures = {};
    $scope.lecture = {};
    $scope.error = {};
    $http.get("/api/lectures").success(function (data, status) {
        $scope.lectures = data;
    });
    $scope.submit = function () {
  
        $http({
            method: 'POST',
            url: "/api/lectures/",
            data: JSON.stringify({ Name: $scope.Name, LectureURL: $scope.LectureURL, Description: $scope.Description })
        }).success(function (data) {
            $http.get("/api/lectures").success(function (data, status) {
                $scope.lectures = data;
                $scope.Name = "";
                $scope.LectureURL = "";
                $scope.Description = "";
            });
        }).error(function (data, status) {
            $scope.error = data;
            $scope.error.code = status;
            $scope.error.show = true;

            
        });
    };

}]);

app.controller('editLectureController', ['$scope', '$http', '$routeParams', '$location', function ($scope, $http, $routeParams, $location) {
    $scope.lecture = {};
    $http.get("/api/lectures/" + $routeParams.id).success(function (data, status) {
        $scope.lecture = data;
    });

    $scope.submit = function () {;
        $http({
            method: 'PUT',
            url: "/api/lectures/" + $routeParams.id,
            data: JSON.stringify({ Name: $scope.lecture.Name, LectureURL: $scope.lecture.LectureURL, Description: $scope.lecture.Description })
        }).success(function () {
            
        }).error(function () {
            $location.path("/lecture/" + $routeParams.id);
        });
        
    }
}]);
app.controller('LectureController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {

    $http.get("/api/lectures/" + $routeParams.id).success(function (data, status) {
        $scope.lecture = data;
    });
    $http.get("/api/lectures/" + $routeParams.id + "/comments").success(function (data, status) {
        $scope.comments = data;
    });
    $scope.submit = function () {
        $http({
            method: 'POST',
            url: "/api/lectures/" + $scope.lecture.ID + "/comments",
            data: JSON.stringify({ commentText: $scope.commentText })
        }).success(function (data) {
            console.log("success");
            $http.get("/api/lectures/" + $scope.lecture.ID + "/comments").success(function (data, status) {
                $scope.comments = data;
            });
        });
    };
    /*for (var i = 0; i<LectureList.list.length;i++){
        if (LectureList.list[i].id == $routeParams.id){
            lecture = LectureList.list[i];
            break;
        }
    }*/

}]);